//
//  CCRSMSCodeViewController.m
//  
//
//  Created by its on 15/5/8.
//
//

#import "CCRSMSCodeViewController.h"
#import "CCRSMSViewModel.h"

@interface CCRSMSCodeViewController ()
@property (nonatomic,strong) UITextField *code;
@property (nonatomic, strong) UIButton *resend;
@property (nonatomic, strong) UIButton *next;
@property (nonatomic, strong) CCRSMSViewModel *viewModel;
@end

@implementation CCRSMSCodeViewController

- (id) initWithViewModel:(CCRSMSViewModel *)viewModel {
  if (self = [super init]) {
    _viewModel = viewModel;
  }
  return self;
}

- (void)viewDidLoad {
  self.title = @"验证码";
  [self setupUI];
  [self setupCSS];
  [self bindUI];
  [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void) setupUI {
  self.view.backgroundColor = RGB(236, 237, 236, 1);
  _code = [[UITextField alloc] init];
  _code.layer.cornerRadius = 5.0f;
  _code.layer.borderWidth = 0.1f;
  _code.placeholder = @"验证码";
  _resend = [UIButton buttonWithType: UIButtonTypeCustom];
  [_resend setTitle:@"60秒重发" forState:UIControlStateNormal];
  [_resend setBackgroundImage:IMAGE(@"btn_yanzheng") forState:UIControlStateNormal];
  _resend.enabled = NO;
  
  _next = [UIButton buttonWithType:UIButtonTypeCustom];
  [_next setTitle:@"下一步" forState:UIControlStateNormal];
  [_next setTitleColor:RGB(255, 255, 255, 1) forState:UIControlStateNormal];
  [_next setBackgroundImage:IMAGE(@"btn_normal") forState:UIControlStateNormal];

  [self.view addSubview: self.code];
  [self.view addSubview: self.resend];
  [self.view addSubview: self.next];
}

- (void) setupCSS {
  [_code mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view).offset(20);
    make.top.equalTo(self.view).offset(20);
    make.right.equalTo(self.resend.mas_leading).offset(-5);
    make.height.mas_equalTo(40);
  }];
  
  [_resend mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.view).offset(20);
    make.right.equalTo(self.view).offset(-20);
    make.height.mas_equalTo(40);
    make.width.mas_equalTo(150);
  }];
  
  [_next mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view).offset(20);
    make.top.equalTo(self.code.mas_bottom).offset(20);
    make.right.equalTo(self.view).offset(-20);
    make.height.mas_equalTo(40);
  }];
}

- (void) bindUI {
  [[[RACSignal interval:1.0f onScheduler:[RACScheduler currentScheduler]] take:60] subscribeNext:^(id x) {
    [_resend setTitle:[NSString stringWithFormat:@"%d秒后重发",self.viewModel.tick--] forState:UIControlStateNormal];
    if (self.viewModel.tick == 0) {
      [_resend setTitle:@"重新发送" forState:UIControlStateNormal];
      self.resend.enabled = YES;
    }
  } completed:^{
  }];
  
  self.resend.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
    self.viewModel.tick = 60;
    [[[RACSignal interval:1.0f onScheduler:[RACScheduler currentScheduler]] take:60] subscribeNext:^(id x) {
      [_resend setTitle:[NSString stringWithFormat:@"%d秒后重发",self.viewModel.tick--] forState:UIControlStateNormal];
      if (self.viewModel.tick == 0) {
        [_resend setTitle:@"重新发送" forState:UIControlStateNormal];
        self.resend.enabled = YES;
      }
    } completed:^{
    }];
    return [self.viewModel resendSMSCode];
  }];
  
  self.next.rac_command = [[RACCommand alloc]
      initWithEnabled:[self.code.rac_textSignal map:^id(NSString *value) {
        return @(value.length > 0);
      }] signalBlock:^RACSignal *(id input) {
        [CCProgressHUD showHUBInView:self.view withTimeOut:0 str:@"请稍候"];
        [[self.viewModel verfitySMSCode:self.code.text] subscribeNext:^(id x) {
          [CCRouter routerWithURL:@"ruser" param:nil];
          [CCProgressHUD hiddenHUBFromView:self.view];
        } error:^(NSError *error) {
          [CCProgressHUD showHUBInView:self.view withTimeOut:2.0f str:@"出错了"];
        } completed:^{
        }];
        return [RACSignal empty];
      }];
}
@end
